#! /usr/bin/sdlbrt
'- from Paulo Silva, GPL license, december '10
'-   captures picture from a webcam, converts to CoCo2 .bin (.cas), and sends the .cas sound to a CoCo2 via mic cable
'- dependencies:
'-   sudo apt-get install perl python sox imagemagick sdlbrt fswebcam
'-   sudo apt-get install xawtv vgrabbj music123 lame mpg123 sdlbasic
'- needed:
'-   a linux computer with a working webcam (like using uvcvideo) and all needed packages installed
'-   a coco2 running a simple cloadm code into a pmode, and connected by audio cable to a linux computer (like on a cassette player)
'-   sdlbasic for running this code
'- the code converts a 64x32 picture into a 'screen0' binary file
'---------
'-- define cluster
dim clust[8,8],cor[16]
a$="00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12"
for y2=0 to 3:for x2=0 to 3:lc=(y2*4)+x2
clust[x2,y2]=val(mid$(a$,(lc*3)+1,2)):next:next
cor[0]=0x000000:cor[1]=0x0000FF:cor[2]=0xFF0000:cor[3]=0xFF00FF
cor[4]=0x00FF00:cor[5]=0x00FFFF:cor[6]=0xFFFF00:cor[7]=0xFFFFFF
'- functions
function primpoint(xq,yq,pq)
  uq=point(xq,yq)
  uq2=bitwiseand(uq,255)
  if pq=1 then:
    uq2=bitwiseand(int(uq/256),255):end if
  if pq=2 then:
    uq2=bitwiseand(int(uq/65536),255):end if
  return uq2
  end function
function difrgbcel(x1q,y1q,x2q,y2q,xsq,ysq)
  tdif=0
  for yqt=0 to ysq-1:for xqt=0 to xsq-1:for pqt=0 to 2
    tdif+=abs(primpoint(x1q+xqt,y1q+yqt,pqt)-primpoint(x2q+xqt,y2q+yqt,pqt))
    next:next:next
  return tdif
  end function
setdisplay(128,64,32,1):paper(0x7f7f7f):ink(0):pen(0):cls
'- draws blocks
ink(0x000000):bar(64,0,128,31)
ink(0x00FF00):bar(64,0,128,7):ink(0xFFFF00):bar(96,0,128,7)
ink(0x0000FF):bar(64,2,128,7):ink(0xFF0000):bar(96,2,128,7)
ink(0xFFFFFF):bar(64,4,128,7):ink(0x00FFFF):bar(96,4,128,7)
ink(0xFF00FF):bar(64,6,128,7):ink(0xFF7F00):bar(96,6,128,7)
ink(0):for x1=0 to 63 step 8: for y1=0 to 7 step 2
for x2=0 to 2:dot(64+x1+x2,y1+1):next:dot(64+x1+5,y1+1):next:next
for x1=0 to 63 step 32: for y1=0 to 7 step 2
for x2=0 to 8:dot(64+x1+x2,y1):next:dot(64+x1+10,y1):dot(64+x1+12,y1):dot(64+x1+14,y1)
for x2=17 to 23 step 2:dot(64+x1+x2,y1):next:next:next
captool$="fswebcam"
fout$="_.bin"
'- starts webcam loop
while 0=0
  if captool$="vgrabbj" then
    setcaption("dummy")
    shell("vgrabbj -d /dev/video0 -F 4 -f _1.jpg")
    setcaption("del")
    shell("rm _1.jpg")
    setcaption("webcam")
    shell("vgrabbj -d /dev/video0 -F 4 -f _1.jpg")
    end if
  if captool$="fswebcam" then
    setcaption("del")
    shell("rm _1.jpg")
    setcaption("webcam")
    shell("fswebcam -d /dev/video0 _1.jpg")
    end if
  setcaption("imagemagick")
  shell("convert _1.jpg -resize 64x32\\! _2.jpg")  '- shell("convert _tmpr1.jpg -resize 64x32\\! -level 0,80%,1 _tmpr2.jpg")
  setcaption("del")
  shell("rm _1.jpg")
  setcaption("load")
  loadimage("_2.jpg",1):pasteicon(0,0,1)
  setcaption("del")
  shell("rm _2.jpg")
  setcaption("conv")

  '- halftone
  for y=0 to 31
    for x=0 to 63
      u=point(x,y)
      b=u mod 256:u=int(u/256)
      g=u mod 256:u=int(u/256)
      r=u mod 256
      patgf=int(((clust[x mod 4,y mod 4]+1)*255)/16)
      rpat=0:if r>patgf then:rpat=1:end if
      gpat=0:if g>patgf then:gpat=1:end if
      bpat=0:if b>patgf then:bpat=1:end if
      o4b=(bpat+(rpat*2)+(gpat*4)) mod 8
      ink(cor[o4b]):dot(x,y+32)
      next:next

  '- text character seeking
  open fout$ for output as #1
  for y1=0 to 15:for x1=0 to 31
    cdif=9999
    'setcaption(str$(x1)+":"+str$(y1)+str$(cdif))
    afi=128
    for a2=0 to 3:for a1=0 to 31:afn=(a2*32)+a1+128
      zdif=difrgbcel((x1*2),32+(y1*2),64+(a1*2),(a2*2),2,2)
      if zdif<cdif then:
        cdif=zdif:afi=afn
        for y2=0 to 1:for x2=0 to 1
          u=point(64+(a1*2)+x2,(a2*2)+y2)
          ink(u):dot(64+(x1*2)+x2,32+(y1*2)+y2)
          next:next
        end if
      next:next
    writebyte(1,afi)    
    next:next
  close #1
  '- saves the image
  grab (1,0,0,128,64):saveimage("_.bmp",1):shell ("convert _.bmp _.png && rm _.bmp")
  setcaption("bin2cas...")
  shell("python bin2cas.py "+fout$)
  setcaption(".cas log...")
  shell("cat "+fout$+".cas >> _log.cas")
  setcaption("cas2wav...")
  foutw$="_.wav"
  shell("perl cas2wav.pl "+fout$+".cas "+foutw$ )
  setcaption("erasing temporary files...")
  shell("rm _.bin _.bin.cas _.png")
  setcaption("sox play...")
  shell("play _.wav")
  setcaption("erasing temporary files...")
  shell("rm _.wav")
  wend
